package com.lgCore.client;

import com.lgCore.client.workHandler.ClientRpcData;
import com.lgCore.factory.RpcServiceZkFactory;
import com.lgCore.model.SystemActiveEnum;
import com.lgCore.model.SystemRegionEnum;
import com.lgCore.util.IpAddr;

import java.net.SocketException;

/**
 * Rpc Tcp 客户端总入口
 */
public class SxRpcTcpClient implements RpcClient {
    /**
     * zk地址
     */
    private String zkServiceAddress;
    /**
     * rpc区域
     */
    private String rpcSystemRegion;
    /**
     * 项目运行环境 dev，test，prod
     */
    private String profilesActive;

    public SxRpcTcpClient(String zkServiceAddress,String rpcSystemRegion,String profilesActive) {
        this.zkServiceAddress = zkServiceAddress;
        this.rpcSystemRegion = rpcSystemRegion;
        this.profilesActive = profilesActive;

        //将数据装载到公共变量，用于业务使用
        ClientRpcData.setRpcSystemRegion(rpcSystemRegion);
        ClientRpcData.setProfilesActive(profilesActive);
        try {
            ClientRpcData.setLocalIp(IpAddr.getLinuxLocalIp());
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    public SxRpcTcpClient(String zkServiceAddress,String rpcSystemRegion) {
        this.zkServiceAddress = zkServiceAddress;
        this.rpcSystemRegion = rpcSystemRegion;
    }
    public SxRpcTcpClient(String zkServiceAddress) {
        this.zkServiceAddress = zkServiceAddress;
    }

    @Override
    public void stop() throws Exception {
//        RpcServiceZkFactory.getZookServiceClient().close();
    }
    @Override
    public void start() throws Exception {
//        //启动连接zk
//        RpcServiceZkFactory.getZookServiceClient().connectZookeeper(zkServiceAddress, sessionTimeOut);
//        //初始化节点
//        RpcServiceZkFactory.getZookServiceClient().initServer();
//        //监控节点信息
//        RpcServiceZkFactory.getZookServiceClient().monitorZkNode();
//        //编码解码 kyro方式
//        ProtocolCore.kyroClass();

        //打开zk客户端
        RpcServiceZkFactory.openConnection(zkServiceAddress,SystemRegionEnum.getOfType(rpcSystemRegion).getType(),SystemActiveEnum.getOfType(profilesActive).getType());

        //初始化配置
        RpcServiceZkFactory.getZookServiceClient().initConfig(RpcServiceZkFactory.getZkRootPath(),RpcServiceZkFactory.getClient());

        //初始化节点
        RpcServiceZkFactory.getZookServiceClient().initServer();
        //监控节点信息
        RpcServiceZkFactory.getZookServiceClient().monitorZkNode();
        //编码解码 kyro方式
//        ProtocolCore.kyroClass();
    }


}
